/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaDeTitulos.java
 *
 * Created on 13/08/2012, 23:32:03
 */
package gui;

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Lindemberg
 */
public class JanelaDeTitulos extends javax.swing.JFrame {

    private int linhas, colunas;
    private ArrayList<PainelDeTitulos> lista;
    private ArrayList<String> titulos;
    private JanelaDeDadosDasEtapas j;
    private JanelaDeDadosDasEtapasEditar j1;
    private boolean editar;
    
    /** Creates new form JanelaDeTitulos */
    public JanelaDeTitulos(int colunas, JanelaDeDadosDasEtapas j) {
        initComponents();
        this.j = j;
        lista = new ArrayList<>();
        titulos = new ArrayList<>();
        this.linhas = colunas;
        this.colunas = 1;
        this.jPanel1.setLayout(new GridLayout((linhas), colunas));
        setarDados();
        this.jPanel1.doLayout();
        this.jScrollPane1.doLayout();
        this.doLayout();
        this.jPanel1.revalidate();
        this.jPanel1.repaint();
        editar = false;
    }

    JanelaDeTitulos(int colunas, JanelaDeDadosDasEtapasEditar j1, ArrayList<String> titulos) {
        initComponents();
        this.linhas = colunas;
        this.colunas = 1;
        this.j1 = j1;
        this.titulos = titulos;
        lista = new ArrayList<PainelDeTitulos>();
        this.jPanel1.setLayout(new GridLayout((linhas), colunas));
        setarDadosEditar();
        this.jPanel1.doLayout();
        this.jScrollPane1.doLayout();
        this.doLayout();
        this.jPanel1.revalidate();
        this.jPanel1.repaint();
        editar = true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jbOk = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.GridLayout(0, 1));
        jScrollPane1.setViewportView(jPanel1);

        jbOk.setText("Ok");
        jbOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbOkActionPerformed(evt);
            }
        });

        jbCancelar.setText("Cancelar");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbOk, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbOk)
                    .addComponent(jbCancelar))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbOkActionPerformed
        System.out.println("Numero de linhas/colunas" + lista.size());
        for(Iterator i = lista.iterator(); i.hasNext();){
            PainelDeTitulos p = (PainelDeTitulos)i.next();
            titulos.add(p.getTitulo());
        }
        if(editar){
            j1.setTitulos(titulos);
        }else{
            j.setTitulos(titulos);
        }
        
        this.dispose();
    }//GEN-LAST:event_jbOkActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbCancelarActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbOk;
    // End of variables declaration//GEN-END:variables

    private void setarDados() {
        for(int i = 1; i <= linhas; i++){
            PainelDeTitulos p = new PainelDeTitulos("Coluna " + i);
            lista.add(p);
            jPanel1.add(p);
        }
    }

    private void setarDadosEditar() {
        if(!titulos.isEmpty()){
            for(int i = 0; i < linhas; i++){
                PainelDeTitulos p = new PainelDeTitulos("Coluna " + (i+1), titulos.get(i));
                lista.add(p);
                jPanel1.add(p);
            }
        }else{
            for(int i = 0; i < linhas; i++){
                PainelDeTitulos p = new PainelDeTitulos("Coluna " + (i+1), "");  
                lista.add(p);
                jPanel1.add(p);
            }
        }
    }
}
